﻿using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Liberate.VSIntergration.Commands;
using Microsoft.VisualStudio.Shell;
using System;
using System.Reflection;

namespace Liberate.VSIntergration
{
    /// <summary>
    /// 
    /// </summary>
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
    [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
    [ProvideLoadKey("Standard", "1.0", "Liberate", "Kong", 1)]
    [ProvideMenuResource(1000, 1)]
    [Guid(GuidList.GuidString_Package)]
    //当vs中存在解决方案的时候加载本Package
    [ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}")]
    public sealed class LiberatePackage : Package
    {
        //static LiberatePackage()
        //{
        //    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(delegate(object sender, ResolveEventArgs args)
        //    {

        //        string requestedName = args.Name;

        //        foreach (Assembly loadedAssembly in AppDomain.CurrentDomain.GetAssemblies())
        //        {

        //            string foundName = loadedAssembly.FullName;

        //            if (requestedName.Equals(foundName))
        //            {

        //                return loadedAssembly;

        //            }

        //        }

        //        return null;

        //    });

        //}

        public LiberatePackage()
        {
            Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this));
        }

        #region Package Members
        
        protected override void Initialize()
        {
            Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this));
            base.Initialize();

            //初始化menu
            InitializeCommands();
        }

        #endregion

        private void InitializeCommands()
        {
            var menuCommandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (menuCommandService != null)
            {
                //todo:以读取配置的形式添加Command
                Command cmd = new GenerateCommand();
                cmd.AttachCommand(menuCommandService);
            }
        }
    }
}